package com.erp.pull

import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.Myapp
import com.erp.mclass.UnCeHandler
import com.erp.util.Myutil

/**
 * Created by Administrator on 2019/3/18/018.
 * 作者：刘洋
 */
abstract class BasePullP :PullP{
    var key =""
    var page =0
    var pagesize =20
    lateinit var PullV : PullView
    override fun xiala() {
        UnCeHandler.buzhou = "xiala"
        page=0
        getData(page,pagesize,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                UnCeHandler.buzhou = "xiala1"
                if(data==null){
                    PullV.xialaNo()
                    return
                }
                if(key!="") {
                    Myapp.editor.putString(key, data)
                    Myapp.editor.commit()
                }
                listClean()
                jsondata(data)
                PullV.xialaOk()
            }

            override fun onNot(throwable: String?) {

                PullV.xialaNo()
            }

        })
    }

    override fun shangla() {
        page++
        UnCeHandler.buzhou = "shangla"
        getData(page,pagesize,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                UnCeHandler.buzhou = "shangla1"
                if(data==null){
                    PullV.shanglaNo()
                    return
                }
                if (data == "[]") {
                    Myutil.MyTost("已经是全部数据")
                }
                jsondata(data)
                PullV.shanglaOk()
            }

            override fun onNot(throwable: String?) {
                Log.d("onNot","throwable="+throwable)
                PullV.shanglaNo()
            }

        })
    }

    override fun gainData(key: String) {
        UnCeHandler.buzhou = "gainData"
        this.key=key
        val data = Myapp.sp.getString(key, "")
        jsondata(data)
        PullV.gainDataOk()
        UnCeHandler.buzhou = "gainData1"
    }

    protected abstract fun getData(page : Int ,pagesize :Int,onUserDataInterface: OnUserDataInterface)

    protected abstract fun jsondata(data :String?)

    protected abstract fun listClean()

}